// packageC/pages/package/agv_manage/getCode/getCode.js
const mybehavior = require("../../../../../behaviors/requests")
var taskId = null;
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    options1: [],
    options2: [],
    AGV_name: "",
    if_online: "",
    data_list: [],
    current: 1,
    page_size: 5,
    department: "",
    page_all: 0,
    total: 0,
    caozuo_show: false,
    change_agv_name: "",
    current_data: {},
    department: "",
    workshop: "",
    area_name: "",

  },
  resetSearchData() {
    this.setData({
      department: "",
      workshop: "",
      area_name: "",
      current: 1,
      page_size: 5,
    })
    this.getAllData()
  },
  toAddLine() {

    wx.navigateTo({
      url: '/packageC/pages/package/agv_manage/controlAreaManage/addControlArea/addControlArea',
    })
    clearInterval(taskId)
  },
  // 删除数据
  onDelete(e) {
    console.log(e.target.dataset.row_data);
    wx.showModal({
      title: '删除',
      content: '确认删除？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          let url = this.data.baseUrl + "/delete_control_area"
          let data = {
            area_name: e.target.dataset.row_data.area_name,
            department: e.target.dataset.row_data.department,
          }
          // console.log("删除数据-传过去的数据", data);
          this.post(url, data).then(res => {
            if (res.code == 20000) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
              this.setData({
                current: 1,
                page_size: 5,
              })
              this.getAllData()
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {
            console.log(err);
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
        }
      }
    })


  },
  // 编辑数据
  updateData() {
    if (this.data.change_agv_name == "") {
      wx.showToast({
        title: '输入内容为空！',
        icon: 'error'
      })
    } else {
      let url = this.data.baseUrl + "/update_magnetic_agv_info"
      let data = {
        agv_num: this.data.current_data.agv_num,
        agv_name: this.data.change_agv_name,
        department: this.data.current_data.department,
      }
      console.log("编辑数据-传过去的数据", data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          this.setData({
            caozuo_show: false,
            current_data: {},
            change_agv_name: ""
          })
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.getAllData()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }
  },
  // 编辑
  editBtn(e) {
    this.setData({
      current_data: e.target.dataset.row_data
    })
    console.log("当前数据", this.data.current_data);
    wx.navigateTo({
      url: '/packageC/pages/package/agv_manage/controlAreaManage/editControlArea/editControlArea',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.target.dataset.row_data)

      }
    })
    clearInterval(taskId)

  },
  onCzCancel() {
    this.setData({
      caozuo_show: false,
      current_data: {},
      change_agv_name: ""
    })
    console.log("当前数据", this.data.current_data);
  },
  // 分页
  onPageChange(e) {
    // console.log("xxx",e.detail.sonParam);
    let p = e.detail.sonParam
    this.setData({
      current: p,
    })
    this.getAllData()
  },
  // 下拉选择
  getPickerVlaue1(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail.name

    })
  },
  // 下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail

    })
    if (key = "department") {
      this.getWorkshopData()

    }
  },

  // 查询
  getAllData() {
    let url = this.data.baseUrl + "/get_control_area_info"
    let data = {
      department: this.data.department,
      workshop: this.data.workshop,
      area_name: this.data.area_name,
      current: this.data.current,
      page_size: this.data.page_size,
    }
    // console.log("传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          data_list: res.data.table_data,
          page_all: Math.ceil(res.data.table_total / this.data.page_size),
          total: res.data.table_total,
        })

      } else {
        this.setData({
          data_list: [],
          page_all: 0,
          total: 0,
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        data_list: [],
        page_all: 0,
        total: 0,
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  // 查询部门信息
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data,
        })

      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询线体
  getWorkshopData() {
    let url = this.data.baseUrl + "/get_workshop_list"

    let data = {
      department: this.data.department,
    }
    // console.log("查询部门-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options2: res.data
        })

      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询按钮
  getAllDataBtn() {
    this.setData({
      current: 1,
      page_size: 5,
    })
    this.getAllData()
  },

  getUnlimited() {
    const page = 'packageC/pages/package/agv_manage/getCode/getCode'
    const _id = "123456"
    const img_src = "https://logisticsengineering.gree.com/gm_user/get_agv_mini_code?page=" + page + '&_id=' + _id + Date.nv_now()
    this.setData({
      code_img: img_src
    })
    console.log(img_src);
    this.previewPhoto1(img_src)

  },
  previewPhoto1(e) {
    var img_url = e
    var u = []
    u.push(img_url)
    wx.previewImage({
      current: img_url,
      urls: u,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 生成小程序码

  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAllData()
    this.getDepartmentData()
    taskId = setInterval(this.getAllData, 5000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

    clearInterval(taskId)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})